package com.atguigu.jxc.service.impl;


import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    private SupplierDao supplierDao;
    @Override
    public List<Supplier> getList(Integer page, Integer rows, String supplierName) {
        List<Supplier> list = supplierDao.getList(page,rows,supplierName);
        return list;
    }

    @Override
    public String save(Supplier supplier, String supplierId) {
        Integer update = null;
        Integer insert = null;
        ServiceVO serviceVO = null;
        if (supplierId!=null){
            update = supplierDao.update(supplier.getSupplierName(),supplier.getContacts(),supplier.getPhoneNumber(),supplier.getAddress(),supplier.getRemarks(),supplierId);
        }else {
            insert = supplierDao.insert(supplier);
        }
        if (update!=null){
            serviceVO = new ServiceVO(100,"修改成功");
        }
        if (insert!=null){
            serviceVO = new ServiceVO(100,"添加成功");
        }
        Gson gson = new Gson();
        String result = gson.toJson(serviceVO);
        return result;
    }

    @Override
    public String delete(String[] ids) {
        Integer delete = null;
        ServiceVO serviceVO = null;
        if (ids.length!=0){
            delete = supplierDao.delete(ids);
        }
        if (delete!=null){
            serviceVO = new ServiceVO(100,"删除成功");
        }
        Gson gson = new Gson();
        String result = gson.toJson(serviceVO);
        return result;
    }


}
